package com.jianggy.chainofrespon.json;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jianggy
 * @time 2018年9月12日 上午10:53:15
 * @description
 */
public class HandlerChain implements Handler {
	private List<Handler> handlers = new ArrayList<>();
	private int index = 0;
	
	
	public HandlerChain() {
	}
	public HandlerChain(Handler...handlerArray) {
		for (int i = 0; i < handlerArray.length; i++) {
			handlers.add(handlerArray[i]);
		}
	}

	@Override
	public void doParse(JsonRequest request, JsonResponse response,HandlerChain chain) {
		if (index >= handlers.size()) {
			return;
		}
		Handler handler = handlers.get(index);
		index++;
		handler.doParse(request, response,chain);
	}

	public void addHandler(Handler handler) {
		handlers.add(handler);
	}

}
